package com.jerei.plugin.dao.impl;

import com.jerei.plugin.dao.IWcmCommonParamDetailDao;
import com.jerei.plugin.entity.WcmCommonParamDetail;
import com.jerei.plugin.utils.JrJdbcTemplate;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionTools;
import com.jerei.utils.condition.SQLEntity;
import com.jerei.utils.spring.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/20.
 */
@Repository
public class WcmCommonParamDetailDaoImpl implements IWcmCommonParamDetailDao {

    @Autowired
    private JrJdbcTemplate jrJdbcTemplate;

    @Override
    public WcmCommonParamDetail save(WcmCommonParamDetail entity) {
        final String sql = " insert into wcm_common_param_detail (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,sid,param_id,name,val,is_show,remark,orderno) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        final WcmCommonParamDetail finalEntity = entity;
        this.jrJdbcTemplate.getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setObject(1, finalEntity.getUuid());
                ps.setObject(2, new Timestamp(finalEntity.getAddDate().getTime()));
                ps.setObject(3, finalEntity.getAddIp());
                ps.setObject(4, finalEntity.getAddUser());
                ps.setObject(5, new Timestamp(finalEntity.getUpdateDate().getTime()));
                ps.setObject(6, finalEntity.getUpdateIp());
                ps.setObject(7, finalEntity.getUpdateUser());
                ps.setObject(8, finalEntity.getSid());
                ps.setObject(9, finalEntity.getParamId());
                ps.setObject(10, finalEntity.getName());
                ps.setObject(11, finalEntity.getVal());
                ps.setObject(12, finalEntity.getIsShow());
                ps.setObject(13, finalEntity.getRemark());
                ps.setObject(14, finalEntity.getOrderno());
                return ps;
            }
        }, keyHolder);
        Long id = keyHolder.getKey().longValue();
        return this.find(id);
    }

    @Override
    public int delete(Long id) {
        final String sql = " delete from wcm_common_param_detail where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                id
        });
    }

    @Override
    public int update(WcmCommonParamDetail entity) {
        final String sql = " update wcm_common_param_detail set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,sid=?,param_id=?,name=?,val=?,is_show=?,remark=?,orderno=? where id = ?";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                entity.getUuid(),
                entity.getAddDate(),
                entity.getAddIp(),
                entity.getAddUser(),
                entity.getUpdateDate(),
                entity.getUpdateIp(),
                entity.getUpdateUser(),
                entity.getSid(),
                entity.getParamId(),
                entity.getName(),
                entity.getVal(),
                entity.getIsShow(),
                entity.getRemark(),
                entity.getOrderno(),
                entity.getId()
        });
    }

    @Override
    public WcmCommonParamDetail find(Long id) {
        final String sql = " select * from wcm_common_param_detail where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new WcmCommonParamDetailRowerMapper());
    }

    @Override
    public void save(List<WcmCommonParamDetail> list) {
        if (null != list && list.size() > 0) {
            final String sql = " insert into wcm_common_param_detail (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,sid,param_id,name,val,is_show,remark,orderno) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonParamDetail entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[14];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getSid();
                objArr[8] = entity.getParamId();
                objArr[9] = entity.getName();
                objArr[10] = entity.getVal();
                objArr[11] = entity.getIsShow();
                objArr[12] = entity.getRemark();
                objArr[13] = entity.getOrderno();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void delete(List<Long> ids) {
        if (null != ids && ids.size() > 0) {
            final String sql = " delete from wcm_common_param_detail where id = ? ";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            for (int i = 0; i < ids.size(); i++) {
                objArr = new Object[1];
                objArr[0] = ids.get(i);
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void update(List<WcmCommonParamDetail> list) {
        if (null != list && list.size() > 0) {
            final String sql = " update wcm_common_param_detail set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,sid=?,param_id=?,name=?,val=?,is_show=?,remark=?,orderno=? where id = ?";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonParamDetail entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[14];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getSid();
                objArr[8] = entity.getParamId();
                objArr[9] = entity.getName();
                objArr[10] = entity.getVal();
                objArr[11] = entity.getIsShow();
                objArr[12] = entity.getRemark();
                objArr[13] = entity.getOrderno();
                objArr[14] = entity.getId();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public PageBean<WcmCommonParamDetail> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        SQLEntity sqlEntity = ConditionTools.conditionEntities2SQL(conditionEntity);
        final String sql = " select * from wcm_common_param_detail where 1 = 1 " + sqlEntity.getSql() + " order by " + sqlEntity.getOrder();
        return new PageBean<WcmCommonParamDetail>(sql, sqlEntity.getParams(), nowPage, pageSize, new WcmCommonParamDetailRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }

    @Override
    public List<WcmCommonParamDetail> getByParamId(Long paramId) {
        final String sql = " select id,name,val from wcm_common_param_detail where is_show = 1 and param_id = ? order by orderno asc , id desc ";
        return this.jrJdbcTemplate.getJdbcTemplate().query(sql, new Object[]{
                paramId
        }, new WcmCommonParamDetailRowerMapper());
    }

    @Override
    public List<WcmCommonParamDetail> getByAll() {
        final String sql = " select id,name,val from wcm_common_param_detail where is_show = 1 order by orderno asc , id desc ";
        return this.jrJdbcTemplate.getJdbcTemplate().query(sql, new WcmCommonParamDetailRowerMapper());
    }

}


class WcmCommonParamDetailRowerMapper implements RowMapper<WcmCommonParamDetail> {
    @Override
    public WcmCommonParamDetail mapRow(ResultSet rs, int rowNum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        WcmCommonParamDetail entity = new WcmCommonParamDetail();
        if (fieldSet.contains("id")) {
            entity.setId(rs.getLong("id"));
        }
        if (fieldSet.contains("uuid")) {
            entity.setUuid(rs.getString("uuid"));
        }
        if (fieldSet.contains("add_date")) {
            entity.setAddDate(new java.util.Date(rs.getTimestamp("add_date").getTime()));
        }
        if (fieldSet.contains("add_ip")) {
            entity.setAddIp(rs.getString("add_ip"));
        }
        if (fieldSet.contains("add_user")) {
            entity.setAddUser(rs.getString("add_user"));
        }
        if (fieldSet.contains("update_date")) {
            entity.setUpdateDate(new java.util.Date(rs.getTimestamp("update_date").getTime()));
        }
        if (fieldSet.contains("update_ip")) {
            entity.setUpdateIp(rs.getString("update_ip"));
        }
        if (fieldSet.contains("update_user")) {
            entity.setUpdateUser(rs.getString("update_user"));
        }
        if (fieldSet.contains("sid")) {
            entity.setSid(rs.getLong("sid"));
        }
        if (fieldSet.contains("param_id")) {
            entity.setParamId(rs.getLong("param_id"));
        }
        if (fieldSet.contains("name")) {
            entity.setName(rs.getString("name"));
        }
        if (fieldSet.contains("val")) {
            entity.setVal(rs.getString("val"));
        }
        if (fieldSet.contains("is_show")) {
            entity.setIsShow(rs.getInt("is_show"));
        }
        if (fieldSet.contains("remark")) {
            entity.setRemark(rs.getString("remark"));
        }
        if (fieldSet.contains("orderno")) {
            entity.setOrderno(rs.getInt("orderno"));
        }
        return entity;
    }
}